<template>
  <div class="Menu">
    <div class="logo" :class="!state.isCollapse ? '' : 'close-style'">
      <img :src="state.log" alt="show">
      <span :style="{opacity: !state.isCollapse ? '1' : '0'}">IP流量分析系统</span>
    </div>
    <el-menu
      :class="{
        'el-menu-vertical': mode === 'vertical',
      }"
      :mode="mode"
      :collapse="state.isCollapse"
      router
      :default-active="'/' + route.meta.belongMenu"
      :unique-opened="true"
      :background-color="background"
      :text-color="text"
      :active-text-color="activeText"
    >
      <template
        v-for="(level_1, level_1_index) in data"
        :key="level_1_index"
        class="menu-item noSelect"
      >
        <!-- level 1 -->
        <el-menu-item v-if="level_1.path" :index="level_1.path">
          <i :class="level_1.icon" class="iconfont iconfont-m"></i>
          <template #title>
            <span>{{ level_1.title }}</span>
          </template>
        </el-menu-item>

        <!-- level 1 -->
        <el-sub-menu v-if="!level_1.path" :index="String(level_1_index)" popper-class="test">
          <!-- level 1 -->
          <template #title>
            <i :class="level_1.submenu!.icon" class="iconfont iconfont-m"></i>
            <span v-show="!state.isCollapse">
              {{ level_1.submenu!.submenuTitle }}
            </span>
          </template>

          <!-- level 2 -->
          <template
            v-for="(level_2, leve_2_Index) in level_1.submenu!.submenuItems"
            :key="leve_2_Index"
          >
            <!-- level 2 -->
            <el-menu-item v-if="level_2.path" :index="level_2.path">
              <template #title>
                <i v-show="!state.isCollapse" :class="level_2.icon" class="iconfont iconfont-m"></i>
                <span>{{ level_2.title }}</span>
              </template>
            </el-menu-item>

            <!-- level 2 -->
            <el-sub-menu index="submenuIndex" v-if="!level_2.path">
              <!-- level 3 -->
              <i :class="level_2.submenu!.icon" class="iconfont iconfont-m"></i>
              <template #title>
                <span>{{ level_2.submenu!.submenuTitle }}</span>
              </template>

              <!-- level 3 -->
              <el-menu-item
                v-for="(level_3, level_3_index) in level_2.submenu!.submenuItems"
                :key="level_3_index"
                :index="level_3.path"
              >
                <i :class="level_3.icon" class="iconfont iconfont-m"></i>
                <template #title>
                  <span>{{ level_3.title }}</span>
                </template>
              </el-menu-item>
            </el-sub-menu>
          </template>
        </el-sub-menu>
      </template>

      <!-- 展开和关闭 -->
      <!-- <div class="collapse" @click="handleCollapse" v-if="isHasCollapseBtn">
        <el-icon :size="20" v-if="!isCollapse">
          <Back />
        </el-icon>
        <el-icon :size="20" v-else>
          <Right />
        </el-icon>
      </div> -->
    </el-menu>
  </div>
</template>

<script lang="ts" setup>
import type { IMenuData } from "@/config/project-base/menu";
import { useRoute } from "vue-router";
import flows from "../../assets/img/icon-logo.png";
import mitter from '../../config/third-plugins/eventBus'

// 菜单props的类型  注意这个是不能外部导入 否则会出错
interface IMenuProps {
  data: IMenuData[];
  mode?: string;
  isCollapse?: boolean;
  background?: string;
  text?: string;
  activeText?: string;
  isHasCollapseBtn?: boolean;
}

interface menuInfo {
  log:any,
  isCollapse:boolean
}

const state = reactive<menuInfo>({
  log: flows,
  isCollapse: false
})

// emit接受的类型
// interface IEmit {
//   (e: "handleCollapse"): void;
// }

// 设置props的默认值
withDefaults(defineProps<IMenuProps>(), {
  mode: "vertical",
  isCollapse: false,
  background: "#001529",
  text: "#fff",
  activeText: "#fff",
  data: () => [],
  isHasCollapseBtn: true,
});

const route = useRoute();

mitter.on('menuCollapse', (value:any) => {
  state.isCollapse = value
})
// const emit = defineEmits<IEmit>();

// const handleCollapse = () => {
//   emit("handleCollapse");
// };
</script>

<style scoped lang="scss">
.Menu {
  position: relative;
  height: 100%;
  z-index: 9999;

  .project-header {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 32px;
      position: relative;
      bottom: 3px;
    }
    h2 {
      font-size: 18px;
      margin-left: 3px;
      margin-right: 26px;
    }
  }
  .el-menu {
    border: none;
    position: relative;
  }

  .collapse {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
    cursor: pointer;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    border-top: 1px solid var(--primary-border);

    /* background-color: #1b4caf; */
    i {
      /* color: #fff; */
      font-size: 20px;
      font-weight: bold;
    }
  }

  .iconfont-m {
    margin-right: 5px;
  }

  .el-menu-vertical:not(.el-menu--collapse) {
    width: 210px;
  }

  :deep(.el-submenu__icon-arrow) {
    font-size: 17px;
  }
  :deep(.el-menu) {
    height: calc(100% - 50px);
  }
  /* :deep(.is-active) {
    background-color: #415da5 !important;
  } */
  // :deep(.el-sub-menu .el-sub-menu__icon-arrow) {
  //   display: none;
  // }
  :deep(.el-menu-item:hover) {
    background-color: #0960bd;
  }

  :deep(.el-menu-item.is-active) {
    background-color: #0960bd !important;
  }

  :deep(.el-menu-item span) {
    position: relative;
    left: 4px;
  }

  .logo {
    width: 210px;
    transition: width 0.3s;
    -moz-transition: width 0.3s; /* Firefox 4 */
    -webkit-transition: width 0.3s; /* Safari and Chrome */
    -o-transition: width 0.3s; /* Opera */
    padding: 9px 0 9px 15px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    overflow: hidden;
    img {
      width: 32px;
      height: 32px;
      display: inline-block;
      box-sizing: content-box;
    }
    span {
      padding-left: 10px;
      white-space: nowrap;
      display: inline-block;
      font-size: 16px;
      font-weight: 700;
      line-height: normal;
      position: relative;
      top: 2px;
      transition: opacity 0.6s linear;
      opacity: 1;
    }
  }

  .close-style {
    width: 64px !important;
  }
}
</style>
